<?php
	require_once("password.php");
	
	function GetConnection(){
		global $password;
		$conn = new mysqli("localhost","n02432643",$password,"n02432643_db");
		if($conn->error){
			throw new Exception($conn->error, 1);
		}
		return $conn;
	}
	
	function LoginUser($req){
			if(strlen($req['first_name']) > 0){
				if(strlen($req['last_name']) >0){
					$conn = GetConnection();
					$first_name = $req['first_name'];
					$last_name = $req['last_name'];
					$results = $conn->query("SELECT * FROM `Customers` WHERE `first_name`='$first_name' AND `last_name`='$last_name'");;
						$numrows = mysqli_num_rows($results);
					if($results && $numrows != 0){
						$results = $results->fetch_assoc();
						$_SESSION['first_name'] = $results['first_name'];
						$_SESSION['last_name'] = $results['last_name'];
						$_SESSION['id'] = $results['id'];
						$_SESSION['is_logged_in'] = 1;
						$_SESSION['Keyword_id'] = $results['Keyword_id'];
					}
					else
						return false;
				}
				else
					return false;
			}
			else
				return false;
	}
	
	function loggedIn(){
		if(isset($_SESSION['is_logged_in']) && $_SESSION['is_logged_in']==1)
			return true;
		else
			return false;	
	}
	
	function isEmployee(){
		if(loggedIn())
			if($_SESSION['Keyword_id'] == 2 && isset($_SESSION['Keyword_id']))
				return true;
			else
				return false;
		else
			return false;	
	}
	function logout(){
		session_destroy();
		header("Location: http://cs.newpaltz.edu/~N02432643/final/");	
	}

